import {
    View, FlatList, Dimensions, RefreshControl, TouchableOpacity,
    TouchableHighlight, Text, StyleSheet, Image
} from 'react-native';
import React, {PureComponent} from 'react';
import Urls, {getRealUrl} from '../const/Urls'
import LocalImg from '../const/Images'
import moment from 'moment'
import ActionSheet from 'antd-mobile-rn/lib/action-sheet'


class PageJkjh extends PureComponent {

    constructor(props) {
        super(props);
        this.user = this.props.user;
        this.type = 0;
        this.state = {
            data: [],
            isLoading: false,
        }
    }

    componentDidMount() {
        const request = new Map();
        request.set('state', this.type)
        fetch(getRealUrl(Urls['cxsjUrl'], request), {
            method: "GET",
            headers: {
                Accept: "application/json",
                "Content-Type": "application/json"
            },
        })
            .then(response => response.json())
            .then(responseJson => {
                this.setState({
                    data: responseJson
                })
            })
            .catch(error => {
                console.error(error);
            });


    }


    render() {
        return (
            <View>

                <View style={{
                    flexDirection: 'row',
                    flex: 0,
                    height: 60,
                    alignItems: 'center',
                    justifyContent: 'center',
                    backgroundColor: '#0066FF'
                }}>
                    <View style={{flex: 1, padding: 10}}>
                        <TouchableOpacity onPress={() => this.props.navigation.pop()}>
                            <Image source={LocalImg['backImg']} style={{height: 30, width: 30, resizeMode: 'contain'}}/>
                        </TouchableOpacity>
                    </View>
                    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                        <Text style={{fontSize: 20, color: '#ffffff'}}>潮汐数据</Text>
                    </View>

                    <View style={{flex: 1, alignItems: 'flex-end', padding: 10}}>
                    </View>
                </View>


                <FlatList
                    data={this.state.data}
                    renderItem={(item, index) => this.renderRow(item, index)}
                    keyExtractor={this.keyExtractor}
                    ItemSeparatorComponent={this.separator}
                    refreshControl={
                        <RefreshControl
                            refreshing={false}
                        />
                    }/>

            </View>
        )
    }

    keyExtractor = (item, index) => item.INNO + ""

    refresh() {

    }

    separator(index) {
        return (
            <View key={index} style={{height: 1, backgroundColor: '#CCCCCC'}}/>
        )
    }

    processDateTime(date) {
        const m = moment(date)
        if (m.isValid()) {
            return m.format('DD/HHmm')
        } else
            return ""
    }


    renderRow({item, index}) {
        let url = item.url;
        let name = item.name;

        return (

            <TouchableHighlight onPress={() => this.enterDetail({url, name})} key={item.ID}>
                <View style={{
                    overflow: 'hidden',
                    flexDirection: 'row',
                    justifyContent: 'center',
                    alignItems: 'flex-start',
                    backgroundColor: '#F6F6F6',
                    height: 42
                }}>
                    <View style={{marginLeft: 5, marginTop: 10}}>
                        <Text style={{color: '#0066FF'}}>{index + 1}.</Text>
                    </View>

                    <View style={[styles.text, {flex: 1, marginLeft: 10, marginTop: 10, justifyContent: 'center'}]}>
                        <Text style={[styles.text, {fontWeight: 'bold'}]}>
                            {item.name}
                        </Text>


                    </View>
                </View>
            </TouchableHighlight>


        )
    }


    enterDetail(url, name) {
        this.props.navigation.navigate('PageCxsjDetail', {
            url: url,
            name: name,
        })
    }

}

const styles = StyleSheet.create({
    phone: {
        flex: 1,
        textDecorationLine: 'underline',
        color: '#0066FF',
    },
    text: {
        flex: 1
    },
    row: {
        flexDirection: 'column',
        justifyContent: 'center',
        padding: 10,
    },
});


export default PageJkjh;